package client

import (
	. "chess_platform/internal/common"
	"chess_platform/models/client"
	"time"
)

//[client]游戏信息
type GameInfoController struct {
	GrantController
}

func (b *GameInfoController) URLMapping() {
	b.Mapping("PlayerRecord", b.PlayerRecord)
	b.Mapping("ClubRecord", b.ClubRecord)
	b.Mapping("TotalClubRecord", b.TotalClubRecord)
	b.Mapping("V2TotalClubRecord", b.V2TotalClubRecord)
	b.Mapping("V2ClubRecord", b.V2ClubRecord)
	b.Mapping("RecordDetail", b.RecordDetail)
	b.Mapping("GamePlayback", b.GamePlayback)
}

// @Title 获取玩家战绩列表
// @Description 获取玩家战绩列表
// @Param   page_size    body   int     true       "每页显示数量"
// @Param   page_index   body   int     true       "第几页"
// @Param   club_id      body   int     false      "亲友圈id(如果传了亲友圈id,就是玩家所在亲友圈的战绩,不传就是个人所有的战绩)"
// @Param   day          body   int     true       "指定一天的时间戳"
// @Success 0 {string}
// @router /player-record [post]
func (b *GameInfoController) PlayerRecord() {
	type Params struct {
		ListParams
		ClubId int64 `json:"club_id"`
		Day    int64 `json:"day"`
	}
	var p Params
	if !b.DecodeParams(&p) {
		return
	}
	if !b.ValidParams(&p) {
		return
	}

	roomLog, count, err := client.PlayerRecord(p.ListParams, b.Uid, p.ClubId, p.Day)
	if err != nil {
		LogClient.Error(b.DatabaseError(err.Error()))
		return
	}
	b.Res.Count = count
	b.Res.Data = roomLog
	b.ResponseSuccess()
}

// @Title 获取亲友圈战绩列表
// @Description 获取亲友圈战绩列表
// @Param   page_size    body   int     true       "每页显示数量"
// @Param   page_index   body   int     true       "第几页"
// @Param   club_id      body   int     true       "亲友圈id"
// @Param   day          body   int     true       "指定一天的时间戳"
// @Success 0 {string}
// @router /club-record [post]
func (b *GameInfoController) ClubRecord() {
	type Params struct {
		ListParams
		ClubId int64 `json:"club_id" valid:"Required"`
		Day    int64 `json:"day" valid:"Required"`
	}
	var p Params
	if !b.DecodeParams(&p) {
		return
	}
	if !b.ValidParams(&p) {
		return
	}

	roomLog, count, err := client.ClubRecord(p.ListParams, p.ClubId, p.Day)
	if err != nil {
		LogClient.Error(b.DatabaseError(err.Error()))
		return
	}
	b.Res.Count = count
	b.Res.Data = roomLog
	b.ResponseSuccess()
}

// @Title 获取亲友圈当天总战绩列表
// @Description 获取亲友圈当天总战绩列表
// @Param   page_size    body   int     true       "每页显示数量"
// @Param   page_index   body   int     true       "第几页"
// @Param   club_id      body   int     true       "亲友圈id"
// @Param   day          body   int     true       "指定一天的时间戳"
// @Success 0 {string}
// @router /total-club-record [post]
func (b *GameInfoController) TotalClubRecord() {
	type Params struct {
		ListParams
		ClubId int64 `json:"club_id" valid:"Required"`
		Day    int64 `json:"day" valid:"Required"`
	}
	var p Params
	if !b.DecodeParams(&p) {
		return
	}
	if !b.ValidParams(&p) {
		return
	}

	roomLog, count, err := client.TotalClubRecord(p.ListParams, p.ClubId, p.Day)
	if err != nil {
		LogClient.Error(b.DatabaseError(err.Error()))
		return
	}
	table, card := client.ClubTableCard(p.ClubId, p.Day)
	b.Res.Count = count
	b.Res.Data = map[string]interface{}{
		"record": roomLog,
		"club": map[string]interface{}{
			"day_table": table,
			"day_card":  card,
		},
	}
	b.ResponseSuccess()
}

// @Title 获取亲友圈当天总战绩列表(版本2)
// @Description 获取亲友圈当天总战绩列表(版本2)
// @Param   page_size    body   int     true       "每页显示数量"
// @Param   page_index   body   int     true       "第几页"
// @Param   club_id      body   int     true       "亲友圈id"
// @Param   start_time   body   int     false      "起始时间的时间戳(最多只能查询31天)"
// @Param   end_time     body   int     false      "终止时间的时间戳(最多只能查询31天)"
// @Success 0 {string}
// @router /v2/total-club-record [post]
func (b *GameInfoController) V2TotalClubRecord() {
	type Params struct {
		ListParams
		ClubId    int64 `json:"club_id" valid:"Required"`
		StartTime int64 `json:"start_time"`
		EndTime   int64 `json:"end_time"`
	}
	var p Params
	if !b.DecodeParams(&p) {
		return
	}
	if !b.ValidParams(&p) {
		return
	}
	if p.StartTime >= 0 && p.StartTime > p.EndTime {
		b.ResponseFail("结束时间不能小开始时间")
		return
	}
	//31天的时间等于:2678400
	if p.StartTime >= 0 && (p.EndTime-p.StartTime) > 2678400 {
		b.ResponseFail("最多只能查询31天")
	}
	if p.StartTime == 0 || p.EndTime == 0 {
		p.StartTime = time.Now().Unix()
		p.EndTime = time.Now().Unix()
	}

	roomLog, count, err := client.TotalClubRecord2(p.ListParams, p.ClubId, p.StartTime, p.EndTime)
	if err != nil {
		LogClient.Error(b.DatabaseError(err.Error()))
		return
	}
	table, card := client.ClubTableCard2(p.ClubId, p.StartTime, p.EndTime)
	b.Res.Count = count
	b.Res.Data = map[string]interface{}{
		"record": roomLog,
		"club": map[string]interface{}{
			"day_table": table,
			"day_card":  card,
		},
	}
	b.ResponseSuccess()
}

// @Title 获取亲友圈战绩列表
// @Description 获取亲友圈战绩列表
// @Param   page_size    body   int     true       "每页显示数量"
// @Param   page_index   body   int     true       "第几页"
// @Param   club_id      body   int     true       "亲友圈id"
// @Param   day          body   int     true       "指定一天的时间戳"
// @Success 0 {string}
// @router /v2/club-record [post]
func (b *GameInfoController) V2ClubRecord() {
	type Params struct {
		ListParams
		ClubId int64 `json:"club_id" valid:"Required"`
		Day    int64 `json:"day" valid:"Required"`
	}
	var p Params
	if !b.DecodeParams(&p) {
		return
	}
	if !b.ValidParams(&p) {
		return
	}

	roomLog, count, err := client.ClubRecord(p.ListParams, p.ClubId, p.Day)
	if err != nil {
		LogClient.Error(b.DatabaseError(err.Error()))
		return
	}
	table, card := client.ClubTableCard(p.ClubId, p.Day)
	b.Res.Count = count
	b.Res.Data = map[string]interface{}{
		"record": roomLog,
		"club": map[string]interface{}{
			"day_table": table,
			"day_card":  card,
		},
	}
	b.ResponseSuccess()
}

// @Title 获取战绩详情
// @Description 获取战绩详情
// @Param   table_id  	   body    int  true      "桌子ID"
// @Success 0
// @Failure 400
// @Failure 404
// @router /record-detail [post]
func (b *GameInfoController) RecordDetail() {
	type Params struct {
		TableId int64 `json:"table_id"`
	}
	var p Params
	if !b.DecodeParams(&p) {
		return
	}

	detail, err := client.RecordDetail(p.TableId)
	if err != nil {
		LogClient.Error(b.DatabaseError(err.Error()))
		return
	}
	b.Res.Data = detail
	b.ResponseSuccess()
}

// @Title 获取战报回放
// @Description 获取战报回放
// @Param   chess_id  	   body    string  true      "牌局"
// @Success 0
// @Failure 400
// @Failure 404
// @router /game-playback [post]
func (b *GameInfoController) GamePlayback() {
	type Params struct {
		ChessId string `json:"chess_id"`
	}
	var p Params
	if !b.DecodeParams(&p) {
		return
	}
	data, err := client.GamePlayback(p.ChessId)
	if err == MsgErrDataNotFound {
		b.ResponseFail("此回放码不存在")
		return
	}
	if err != nil {
		LogClient.Error(b.SystemError(err.Error()))
		return
	}
	b.Res.Data = data
	b.ResponseSuccess()
}
